<template>
  <div>
  </div>

</template>

<script>

export default {
  name: "HelloView",
  data() {
    return {
      //2属性定义
      title:'',
    }
  },
  mounted() {
    this.initWebSocket()
  },
  unmounted: function() {
    this.websocketclose();
  },
  methods: {
    open() {
      const h = this.$createElement;
      this.$msgbox({
        title: this.title,
        message: h('p', null, [
          h('span', null,  null),
          h('i', { style: 'color: teal' }, null)
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '执行中...';
            this.$router.push('sell')
            setTimeout(() => {
              done();
              setTimeout(() => {
                instance.confirmButtonLoading = false;
              }, 100);
            }, 100);
          } else {
            done();
          }
        }
      }).then(action => {
        this.$message({
          type: 'info',
          message: 'action: ' + action
        });
      })
    },
    initWebSocket: function () {
      let userId = 1;
      let url = "ws://localhost:8182/websocket/"+userId;
      this.websocket = new WebSocket(url);
      this.websocket.onopen = this.websocketonopen;
      this.websocket.send = this.websocketsend;
      this.websocket.onerror = this.websocketerror;
      this.websocket.onmessage = this.websocketonmessage;
      this.websocket.onclose = this.websocketclose;
    },
    websocketonopen: function() {
      console.log("Websocket 连接成功")
    },
    websocketerror: function() {
      console.log("Websocket 连接错误")
    },
    websocketsend: function (e) {
      console.log("Websocket 发送消息错误 ： " + e.message)
    },
    websocketonmessage: function(e) {
      this.title = e.data;
      this.open();
      console.log(e.data);
    },
    websocketclose: function (e) {
      console.log("connection closed (" + e.code + ")")
    }

  },
  props: {
    msg: String
  },
  created() {
    //4.钩子函数
  }
}
</script>

<style scoped>

</style>
